﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{

	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	
	public class CanvasInsertHandler extends CanvasDefaultHandler {
		private var shapeShadow:ShapeObject;
		private var shadowAdded:Boolean;
		
		/**
		 Constructor
		 @param funcArray Array of functions to use here
		 		1: addShapeObject to stage
				2: paint 
		*/
		function CanvasInsertHandler(c:CanvasCore){
			super(c);
			shadowAdded = false;
			shapeShadow = null;
		}
		
		override public function activate(param = null):void{
			// First of all we check if we have any new parameter
			if(param != null){ // If this is the case...
				shapeToCreate = param;
				
				if(shadowAdded){ // If shadow has been added before
					// We remove it from block panel, to add it later
					activated = true;
					this.deactivate();
				}
				// We create a new shapeShadow from the parameter
				shapeShadow = new (getDefinitionByName(param as String) as Class)();
			}
			// If no param was sent, then we will take the one that was
			// last used
			shapeShadow.alpha = 0.3;
			shapeShadow.x = 700+shapeShadow.width;
			
			if(! activated){
				activated = true;
				core.showBlockPanel(true);
				core.getBlockPanel().addChild(shapeShadow);
				shadowAdded = true;
				core.removeSelections();
			}
		}
		
		override public function deactivate():void{
			if(activated){
				if(shapeShadow != null){
					core.getBlockPanel().removeChild(shapeShadow);
					shadowAdded = false;
				}
				activated = false;
			}
		}
		
		override public function mouseMove(evt:MouseEvent):void{
			drawShapeToCreateShadow(evt);
		}
		
		override public function clickFront(evt:MouseEvent):void{
			core.addShapeObj(evt, shapeToCreate);
		}
		
		// ---------------- PRIVATE FUNCTIONS --------------------//
		/**
		  Draws the shapeToCreate under the mouse
		*/
		private function drawShapeToCreateShadow(evt:MouseEvent):void{
			shapeShadow.x = evt.stageX;
			shapeShadow.y = evt.stageY;
		}

	}
}